package com.seckill.search.controller;

import com.seckill.common.domain.dto.PageDTO;
import com.seckill.search.domain.query.ItemPageQuery;
import com.seckill.search.domain.vo.ItemVO;
import com.seckill.search.service.IItemService;
import com.seckill.search.service.ISearchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@RestController
@RequestMapping("items")
@Api(tags = "商品搜索接口")
@RequiredArgsConstructor
public class ItemController {

    private final ISearchService searchService;
    private final IItemService itemeService;

    @ApiOperation("用户端商品搜索接口")
    @GetMapping("/portal")
    public PageDTO<ItemVO> queryItemsForPortal(ItemPageQuery query){
        return searchService.queryItemsForPortal(query);
    }

    @ApiIgnore
    @GetMapping("/name")
    public List<Long> queryItemsIdByName(@RequestParam("keyword") String keyword){
        return searchService.queryItemsIdByName(keyword);
    }

    @ApiOperation("处理指定商品上架失败的问题")
    @PostMapping("/up")
    public void handleItemsUp(
            @ApiParam("商品id集合") @RequestParam("itemIds") List<Long> itemIds) {
        for (Long itemId : itemIds) {
            itemeService.handleItemUp(itemId);
        }
    }

    @ApiOperation("处理指定商品下架失败的问题")
    @PostMapping("/down")
    public void handleItemsDown(
            @ApiParam("商品id集合") @RequestParam("itemIds") List<Long> itemIds) {
            itemeService.handleItemDeletes(itemIds);
    }
}
